package com.stoa.floatlib

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.stoa.floatlib.config.FloatIconConfig
import com.stoa.floatlib.core.AbsStoaFloatView
import com.stoa.floatlib.core.StoaViewLayoutParams

internal class MainIconStoaView(viewTag: String) : AbsStoaFloatView(viewTag) {
    override fun onCreate(context: Context?) {

    }

    override fun onCreateView(context: Context, rootView: FrameLayout?, viewTag: String
    ): View {
        StoaFloat.mContentView?.let {
            val parent = it.parent
            if (parent != null && parent is ViewGroup) {
                parent.removeView(it)
            }
        }
        return StoaFloat.mContentView ?: LayoutInflater.from(context)
            .inflate(R.layout.stoa_icon_layout, rootView, false)
    }

    override fun onViewCreated(rootView: FrameLayout) {
        rootView.setOnClickListener {
            StoaFloat.mainIconClickListener?.mainIconClick(it)
        }
    }

    override fun initStoaFloatViewLayoutParams(params: StoaViewLayoutParams) {
        params.x = FloatIconConfig.getLastPosX(viewTag)
        params.y = FloatIconConfig.getLastPosY(viewTag)
        params.width = StoaFloat.FLOAT_SIZE_WIDTH
        params.height = StoaFloat.FLOAT_SIZE_HEIGHT
    }

    override fun onResume() {
        if (isNormalMode()) {
            val params: FrameLayout.LayoutParams = mFrameLayoutParams!!
            params.width = StoaFloat.FLOAT_SIZE_WIDTH
            params.height = StoaFloat.FLOAT_SIZE_HEIGHT
            invalidate()
        }
    }

    override fun onPause() {

    }

}